#############################################################################
#Yuhua Wang#
#The Rise and Fall of Imperial China: The Social Origins of State Development#
#Princeton University Press, 2022#
############################################################################

############################################################################
#Chapter 8#
############################################################################

######################################
#Figure 8.1: Registered Land during Ming and Qing#
######################################

rm(list = ls())
library(foreign) 
library(ggplot2) 
require(ggplot2)
library(ggrepel) 
library(plyr)
require(scales)
require(gridExtra)

#Change this to your local directory#

setwd("/Users/ywang/Dropbox/Book Project on State Building/Princeton UP/Replication Package/Chapter 8/data/")

land <- read.dta("registered land.dta")

land <- ggplot(land[!is.na(land$land),], aes(year,land)) + 
  geom_point() +
  geom_line(size=1)+
  scale_x_continuous(name="Year", breaks=c(1400,1500,1600,1700,1800,1900)) +
  scale_y_continuous(name="Registered Land (million mu)", limits=c(400,900),breaks=c(400,500,600,700,800,900))+
  theme(legend.position="none")+
  theme_bw()+
  theme(axis.text.x = element_text(color = "grey20", size = 15, family="Times"),
        axis.text.y = element_text(color = "grey20", size = 15, family="Times"),  
        axis.title.x = element_text(color = "grey20", size = 15, family="Times"),
        axis.title.y = element_text(color = "grey20", size = 15, family="Times"))+ 
  geom_vline(xintercept = 1644,color="black",linetype="dashed")+
  annotate(geom="text", x = 1500, y=850, label = "Ming",size=7, family="Times",fontface =2)+
  annotate(geom="text", x = 1800, y=850, label = "Qing", size=7, family="Times",fontface =2)

land

######################################
#Figure 8.2: Mass Rebellion and Elite Collective Action (1800–1900)#
######################################

rm(list = ls())
library(foreign) 
library(ggplot2) 
require(ggplot2)
library(ggrepel) 
library(plyr)
require(scales)
require(gridExtra)

#Change this to your local directory#

setwd("/Users/ywang/Dropbox/Book Project on State Building/Princeton UP/Replication Package/Chapter 8/data/")

taipingts <- read.dta("timeseries 1000-1900.dta")

attach(taipingts)

taiping <- ggplot(data=taipingts, aes(x=year, y=massrebellion, color="black",linetype="solid")) + 
  geom_line(color="black",size=1) +
  scale_x_continuous(name="Year", limits=c(1800,1900), breaks=c(1800,1810,1820,1830,1840,1850,1860,1870,1880,1890,1900)) +
  scale_y_continuous(name="N of Mass Rebellions",limits=c(0,40), breaks=c(0,5,10,15,20,25,30,35,40)) +
  theme_bw()+
  theme(legend.position="none")+
  theme(text = element_text(size=15, 
                            vjust=1, family="Times"))+ 
  geom_vline(xintercept = 1850,color="black",linetype="dashed")+ 
  geom_vline(xintercept = 1869,color="black",linetype="dashed")+ 
  annotate(geom="text", x = 1860, y=40, label = "Taiping",size=5, family="Times",fontface =2)+
  annotate(geom="text", x = 1860, y=36, label = "Rebellion",size=5, family="Times",fontface =2)

genealogy <- ggplot(data=taipingts, aes(x=year, y=genealogy, color="black",linetype="solid")) + 
  geom_line(color="black",size=1) +
  scale_x_continuous(name="Year", limits=c(1800,1900), breaks=c(1800,1810,1820,1830,1840,1850,1860,1870,1880,1890,1900)) +
  scale_y_continuous(name="N of Genealogy Books",limits=c(0,300), breaks=c(0,100,200,300)) +
  theme_bw()+
  theme(legend.position="none")+
  theme(text = element_text(size=15, 
                            vjust=1, family="Times"))+ 
  geom_vline(xintercept = 1850,color="black",linetype="dashed")+ 
  geom_vline(xintercept = 1869,color="black",linetype="dashed")

grid.arrange(taiping, genealogy, ncol=1)
a <- arrangeGrob(taiping, genealogy, ncol=1) 


######################################
#Figure 8.3: Lineage Activity Trends Before and After the Taiping Rebellion#
######################################

#Constructed in Stata#
#See Wang 2022 PUP Chapter 8 replication.do#

######################################
#Figure 8.4: Declarations of Independence (1911)#
######################################

#Constructed in QGIS#

